package com.sne.airways.system.syscache;

import com.sne.airways.basecore.cache.ICacheName;
import com.sne.airways.basecore.cache.IPrefix;
import com.sne.airways.basecore.exceptions.CacheException;
import com.sne.airways.system.pojo.SystemProperties;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.apache.log4j.Logger;

import java.io.Serializable;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: skyline
 * Date: 12-2-17
 * Time: 下午4:17
 * To change this template use File | Settings | File Templates.
 */
public class SystemPropertiesCache extends BaseCache{
    private static Logger logger = Logger.getLogger(SystemPropertiesCache.class.getName());
    private CacheManager cacheManager = null;
    public String getKeyPrefix() {
        return IPrefix.SystemProperties;
    }

    private String getKey(String systemPropertiesId){
        return getKeyPrefix() + systemPropertiesId;
    }

    private boolean isSystemPropertiesKey(String key,Serializable systemPropertiesId){
        String s = (String) systemPropertiesId;
        return key.startsWith(getKeyPrefix() + s);
    }

    /**
     * 注意缓存中可能存在多个systemPropertiesId的，
     * 所以删除的时候利用ehcache search 功能
     * 查询到所有缓存,然后删除掉
     * @param systemPropertiesId
     */
    public void removeIt(String systemPropertiesId){
        List keys = null;
        try {
            keys = getCache().getKeys();
        } catch (net.sf.ehcache.CacheException e) {
            throw new CacheException(e);
        }

        if (keys == null) return;

        for (Iterator it = keys.iterator();it.hasNext();){
            String key = (String) it.next();

            if (isSystemPropertiesKey(key, systemPropertiesId)) {
                getCache().remove(key);
            }
        }
    }

    public SystemProperties getSystemProperties(String systemPropertiesId){
        Element e = null;
        String key = getKey(systemPropertiesId);

        try {
            e = getCache().get(key);
        } catch (Exception e1) {
            throw new CacheException(e1);
        }

        if (e!=null)
            return (SystemProperties) e.getValue();
        else
            throw new CacheException("systemProperties ("+systemPropertiesId+") is null.");
    }

    public Date getCreateTime(String systemPropertiesId){

        Element e = null;
        String key = getKey(systemPropertiesId);

        try {
            e = getCache().get(key);
        } catch (Exception e1) {
            throw new CacheException(e1);
        }

        if (e!=null){
            return new Date(e.getCreationTime());
        }
        else
            throw new CacheException("systemProperties ("+systemPropertiesId+") is null.");
    }

    public void put(String systemPropertiesId,Serializable object){
        Element e = new Element(getKey(systemPropertiesId),object);
        getCache().put(e);
    }

    public Serializable get(String systemPropertiesId){
        return getCache().get(getKey(systemPropertiesId));
    }

    public void create() {

    }

    /**
     * 利用构造函数注入CacheManager
     */
    public SystemPropertiesCache(CacheManager manager) {
        setCache(manager.getCache(ICacheName.CACHE_SYSTEM_PROPERTIES));
        logger.info("[SystemPropertiesCache 服务启动]");
    }

    public int getSystemPropertiesCount() {
        List keys = null;
        try {
            keys = this.getCache().getKeys();
        } catch (net.sf.ehcache.CacheException e) {
            throw new CacheException(e);
        }
        if (keys == null) {
            return 0;
        }
        return keys.size();
    }

    public String getSystemPropertiesValue(String key) {
        SystemProperties systemProperties = this.getSystemProperties(key);
        if (null == systemProperties) {
            return null;
        } else {
            return systemProperties.getValue();
        }
    }

}
